//
//  EmotitionViewController.swift
//  表情栏
//
//  Created by Dengdwd on 15/10/21.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

private let EMOTITION_CELL = "EMOTITION_CELL"


class EmotitionViewController: UIViewController {

    var emotitionClickBlock:((emotition:Emotition) ->())?
    
    init(emotitionBlock:(emotition:Emotition) ->()) {
        emotitionClickBlock = emotitionBlock
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.bounds = CGRectMake(0, 0, SCREENW, 216)
        setUpUI()

    }
    
    
    
    
    
    private func setUpUI() {
        view.addSubview(toolbar)
        toolbar.frame = CGRect(x: 0, y:view.bounds.height - 44, width: SCREENW, height: 44)
        view.addSubview(emotitionView)
        emotitionView.frame = CGRect(x: 0, y: 0, width: SCREENW, height: 216 - 44)
        emotitionView.registerClass(emotitionCell.self, forCellWithReuseIdentifier:EMOTITION_CELL )
        emotitionView.pagingEnabled = true
        emotitionView.bounces = false
        emotitionView.dataSource = self
        emotitionView.delegate = self
        emotitionView.backgroundColor = UIColor.whiteColor()
        emotitionView.showsHorizontalScrollIndicator = false

    }
    @objc private func clickToolBarItem(btn:UIButton) {
      
        let indexPath = NSIndexPath(forItem: 0, inSection: btn.tag)
        emotitionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
    }

    private lazy var emotitionView:UICollectionView = UICollectionView(frame: CGRectZero , collectionViewLayout: emotitionLayout())
    private lazy var emotitionViewModel = EmotitionViewModel.shareEmotitionViewModel


        private lazy var toolbar : UIToolbar = {
        let bar = UIToolbar()
        var items = [UIBarButtonItem]()
        var index = 0
        bar.tintColor = UIColor.grayColor()
        for p in ["最近","默认","emoji","小浪花"] {
            items.append(UIBarButtonItem(title: p, style: UIBarButtonItemStyle.Plain, target: self, action: "clickToolBarItem:"))
            items.last?.tag = index++
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        bar.items = items
        return bar
        }()
}

//数据源方法
extension EmotitionViewController:UICollectionViewDataSource,UICollectionViewDelegate {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return emotitionViewModel.emotitionPakages.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return emotitionViewModel.emotitionPakages[section].emoticons.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EMOTITION_CELL, forIndexPath: indexPath) as! emotitionCell
        cell.emotition = emotitionViewModel.emotition(indexPath)
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        emotitionClickBlock?(emotition: emotitionViewModel.emotition(indexPath))

        emotitionViewModel.favorriteEmotition(indexPath)
        
    }
}

private class emotitionLayout:UICollectionViewFlowLayout {
    private override func prepareLayout() {
        super.prepareLayout()
        let w = SCREENW / 7
        let margin = (collectionView!.bounds.height - 3 * w) * 0.499
        itemSize = CGSize(width: w, height: w)
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        sectionInset = UIEdgeInsets(top: margin, left: 0, bottom: margin, right: 0)
        scrollDirection = .Horizontal
    }
}
private class emotitionCell: UICollectionViewCell {

    var emotition:Emotition? {
        didSet {
        btn.setImage(UIImage(contentsOfFile:emotition!.imagePath), forState: UIControlState.Normal)
        btn.setTitle(emotition!.emoji, forState: UIControlState.Normal)
            if emotition!.isRemove {
            btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(btn)
        btn.frame = CGRectInset(bounds, 4, 4)
        btn.titleLabel?.font = UIFont.systemFontOfSize(32)
        btn.userInteractionEnabled = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var btn = UIButton()

}